<?php

namespace sms\service;

class Antgst extends MessageInterface
{
    private $baseUrl = 'https://api.antgst.com/sms/txt/3/send/json';


    public function sendSms($phone, $message)
    {
        try {
            // 设置 POST 请求的参数
            $data = [
                'authSecret' => $this->account . $this->password,
                'number' =>  $phone,
                'sms' => $message,
            ];
            // 将参数转换为 URL 编码的字符串
            $params_string = json_encode($data);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $this->baseUrl);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER,[
                    'Content-Type: application/json',
                    'Content-Length: ' . strlen($params_string)
                ]
            );
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            $request = curl_exec($ch);
            curl_close($ch);
            $res = json_decode($request);
            if ($res->code == 200) {
                return true;
            } else {
                $error = json_decode($request, true);
                $this->setError( $error['msg']);
                return false;
            }
        } catch (\Exception $e) {
            $this->setError($e->getMessage());
            return false;
        }
    }

    public function getRequestUrl($api)
    {
        // TODO: Implement getRequestUrl() method.
    }
}